package telas.usuarios;

import dao.PerfilDao;
import dao.UsuarioDao;
import java.awt.HeadlessException;
import java.util.Date;
import javax.swing.JOptionPane;
import modelo.Perfil;
import modelo.Usuarios;
import telas.Index;
import util.Utilitario;

/**
 *
 * @author gilberto.costa 26/03/2012 16:43
 */
public class Fr_usuarios extends javax.swing.JInternalFrame {

    private Boolean alterando = false;

    public Fr_usuarios() {
        initComponents();
        Utilitario.eventosteclado(jLabel2, "Usuario", rootPane);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        PassSenhaAtual = new javax.swing.JPasswordField();
        jPasswordField2 = new javax.swing.JPasswordField();
        senhaAtual = new javax.swing.JLabel();
        jPasswordField3 = new javax.swing.JPasswordField();
        jLabel5 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        BtNovo = new javax.swing.JButton();
        btSalvar = new javax.swing.JButton();
        btEditar = new javax.swing.JButton();
        btExcluir = new javax.swing.JButton();
        btSair = new javax.swing.JButton();
        btDesfazer = new javax.swing.JButton();

        setIconifiable(true);
        setTitle("Cadastros de Usuários");

        jPanel2.setBackground(new java.awt.Color(194, 194, 183));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Dados do Usuario", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 0, 255)));

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setText("Nome:  ");
        jLabel2.setPreferredSize(new java.awt.Dimension(37, 25));

        jTextField1.setEditable(false);
        jTextField1.setFont(new java.awt.Font("Cantarell", 0, 15)); // NOI18N
        jTextField1.setForeground(new java.awt.Color(1, 1, 1));
        jTextField1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        jTextField1.setPreferredSize(new java.awt.Dimension(28, 25));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setText("Senha:  ");
        jLabel3.setPreferredSize(new java.awt.Dimension(40, 25));

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel4.setText("Repete Senha:  ");
        jLabel4.setPreferredSize(new java.awt.Dimension(40, 25));

        PassSenhaAtual.setEditable(false);
        PassSenhaAtual.setFont(new java.awt.Font("Cantarell", 0, 15)); // NOI18N
        PassSenhaAtual.setForeground(new java.awt.Color(1, 1, 1));
        PassSenhaAtual.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        PassSenhaAtual.setPreferredSize(new java.awt.Dimension(6, 25));

        jPasswordField2.setEditable(false);
        jPasswordField2.setFont(new java.awt.Font("Cantarell", 0, 15)); // NOI18N
        jPasswordField2.setForeground(new java.awt.Color(1, 1, 1));
        jPasswordField2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        jPasswordField2.setPreferredSize(new java.awt.Dimension(6, 25));

        senhaAtual.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        senhaAtual.setText("Senha Atual: ");

        jPasswordField3.setEditable(false);
        jPasswordField3.setFont(new java.awt.Font("Cantarell", 0, 15)); // NOI18N
        jPasswordField3.setForeground(new java.awt.Color(1, 1, 1));
        jPasswordField3.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        jPasswordField3.setPreferredSize(new java.awt.Dimension(6, 25));

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel5.setText("Código");
        jLabel5.setPreferredSize(new java.awt.Dimension(37, 25));

        jTextField2.setFont(new java.awt.Font("Cantarell", 0, 15)); // NOI18N
        jTextField2.setForeground(new java.awt.Color(1, 1, 1));
        jTextField2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        jTextField2.setEnabled(false);
        jTextField2.setPreferredSize(new java.awt.Dimension(28, 25));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(senhaAtual))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(0, 0, Short.MAX_VALUE)))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(72, 72, 72)))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(PassSenhaAtual, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jPasswordField2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPasswordField3, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE))
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 425, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(29, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(senhaAtual)
                    .addComponent(PassSenhaAtual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPasswordField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPasswordField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel4.setBackground(new java.awt.Color(194, 194, 183));
        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Acoes", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 0, 255)));

        BtNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/Button Add.png"))); // NOI18N
        BtNovo.setMnemonic('n');
        BtNovo.setText("Novo");
        BtNovo.setToolTipText("Inserir Um Novo Usuário - Atalho (ALT + N)");
        BtNovo.setRolloverEnabled(false);
        BtNovo.setVerifyInputWhenFocusTarget(false);
        BtNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtNovoActionPerformed(evt);
            }
        });

        btSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/icontexto-webdev-ok-128x128.png"))); // NOI18N
        btSalvar.setMnemonic('s');
        btSalvar.setText("Salvar");
        btSalvar.setToolTipText("Salvar Usuário - Atalho (ALT + S)");
        btSalvar.setEnabled(false);
        btSalvar.setRolloverEnabled(false);
        btSalvar.setVerifyInputWhenFocusTarget(false);
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });

        btEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/edit.png"))); // NOI18N
        btEditar.setMnemonic('e');
        btEditar.setText("Alterar");
        btEditar.setToolTipText("ALterar Cadastro -  Atalho (ALT + E)");
        btEditar.setEnabled(false);
        btEditar.setRolloverEnabled(false);
        btEditar.setVerifyInputWhenFocusTarget(false);
        btEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditarActionPerformed(evt);
            }
        });

        btExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/Button Delete.png"))); // NOI18N
        btExcluir.setMnemonic('x');
        btExcluir.setText("Excluir");
        btExcluir.setToolTipText("Excluir Usuário - Atalho (ALT + X)");
        btExcluir.setEnabled(false);
        btExcluir.setRolloverEnabled(false);
        btExcluir.setVerifyInputWhenFocusTarget(false);
        btExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExcluirActionPerformed(evt);
            }
        });

        btSair.setBackground(new java.awt.Color(255, 255, 255));
        btSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/Button Previous.png"))); // NOI18N
        btSair.setMnemonic('x');
        btSair.setText("Voltar");
        btSair.setToolTipText("Sair do Cadastro de Usuários - Atalho (ALT + X)");
        btSair.setRolloverEnabled(false);
        btSair.setVerifyInputWhenFocusTarget(false);
        btSair.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentMoved(java.awt.event.ComponentEvent evt) {
                btSairComponentMoved(evt);
            }
        });
        btSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSairActionPerformed(evt);
            }
        });

        btDesfazer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/Button Reload.png"))); // NOI18N
        btDesfazer.setMnemonic('z');
        btDesfazer.setText("Desfazer");
        btDesfazer.setToolTipText("Desfazer Ação - Atalho (ALT + Z)");
        btDesfazer.setEnabled(false);
        btDesfazer.setRolloverEnabled(false);
        btDesfazer.setVerifyInputWhenFocusTarget(false);
        btDesfazer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDesfazerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(BtNovo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btSalvar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btExcluir)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btDesfazer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btSair)
                .addGap(60, 60, 60))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btSair, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(btDesfazer, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btExcluir, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btEditar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btSalvar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(BtNovo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(0, 22, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-652)/2, (screenSize.height-357)/2, 652, 357);
    }// </editor-fold>//GEN-END:initComponents

    private void BtNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtNovoActionPerformed
        alterando = false;
        //limpo os campos
        Utilitario.liberaTodosCampos(rootPane);
        Utilitario.limparTodosCampos(rootPane);

        // oculto os campos de senha atual
        senhaAtual.setVisible(false);
        PassSenhaAtual.setVisible(false);

        //botões
        btSalvar.setEnabled(true);
        btEditar.setEnabled(false);
        btExcluir.setEnabled(false);
        btDesfazer.setEnabled(true);

        //foco
        jTextField1.requestFocus();
    }//GEN-LAST:event_BtNovoActionPerformed

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        try {
            if (jTextField1.getText().trim().equals("")) {
                JOptionPane.showMessageDialog(null, "Informe um nome de usuario");
                jTextField1.requestFocus();
            } else if (jTextField1.getText().length() < 2) {
                JOptionPane.showMessageDialog(null, "O Nome do usuário deve ter mais de 2 caracter");
                jTextField1.requestFocus();
            } else if (jPasswordField3.getText().trim().equals("")) {
                JOptionPane.showMessageDialog(null, "Informe uma senha!!");
                jPasswordField3.requestFocus();
            } else if (jPasswordField2.getText().trim().equals("")) {
                JOptionPane.showMessageDialog(null, "Repita a senha!!");
                jPasswordField2.requestFocus();
            } else if (!jPasswordField3.getText().equals(jPasswordField2.getText())) {
                JOptionPane.showMessageDialog(null, "Senhas não conferem!!!");
                jPasswordField3.requestFocus();
                jPasswordField3.setText(null);
                jPasswordField2.setText(null);
            } else {
                Long idusuario;
                modelo.Usuarios usu;
                Boolean erro;
                if (alterando) {
                    idusuario = Long.parseLong(jTextField2.getText().trim());
                    usu = UsuarioDao.retornaUsuariosPorId(idusuario);
                    if (!usu.getSenha().toString().equalsIgnoreCase(PassSenhaAtual.getText())) {
                        JOptionPane.showMessageDialog(null, "Senha atual não confere!!");
                        erro = true;
                    } else {
                        usu.setAlterado_dia(new Date());
                        usu.setNome_usuario(jTextField1.getText().toString().trim());
                        usu.setSenha(jPasswordField3.getText());
                        erro = false;
                    }
                } else {
                    erro = false;
                    usu = new Usuarios();
                    usu.setNome_usuario(jTextField1.getText());
                    usu.setSenha(jPasswordField3.getText());
                    usu.setDatacadastro(new Date());
                    usu.setAlterado_dia(new Date());
                    Perfil perfil = PerfilDao.retornaPerfilPorId(2L);
                    usu.setPerfil(perfil);
                }
                if (!erro) {
                    usu = usu.salvar();
                    btSalvar.setEnabled(false);
                    btEditar.setEnabled(true);
                    btExcluir.setEnabled(true);
                    btDesfazer.setEnabled(false);
                    //bloquando os campos
                    Utilitario.bloqueiaTodosCampos(rootPane);
                    JOptionPane.showMessageDialog(null, "Cadastro Realizado com Sucesso!!!");
                }
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Erro: " + e);
        }
    }//GEN-LAST:event_btSalvarActionPerformed

    private void btEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditarActionPerformed
        Utilitario.liberaTodosCampos(rootPane);
        // oculto os campos de senha atual
        senhaAtual.setVisible(true);
        PassSenhaAtual.setVisible(true);

        btSalvar.setEnabled(true);
        btExcluir.setEnabled(false);
        btEditar.setEnabled(false);
        btDesfazer.setEnabled(true);
        alterando = true;
    }//GEN-LAST:event_btEditarActionPerformed

    private void btExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExcluirActionPerformed
        try {
            int f;
            Long idUsuario = Long.parseLong(jTextField2.getText().trim());
            Usuarios usuario = UsuarioDao.retornaUsuariosPorId(idUsuario);
            if (usuario != null) {
                String nome = jTextField1.getText();
                f = JOptionPane.showConfirmDialog(this, "Tem Certeza que deseja Excluir o Cadastro do Usuário  ** " + nome + " ** ?", "Confirmação", JOptionPane.YES_NO_OPTION);
                if (f == JOptionPane.YES_OPTION) {
                    //remove o usuario
                    usuario.deleta();

                    Utilitario.limparTodosCampos(rootPane);

                    btEditar.setEnabled(false);
                    btExcluir.setEnabled(false);
                    btDesfazer.setEnabled(false);
                    BtNovo.setEnabled(true);
                    btSalvar.setEnabled(false);

                    JOptionPane.showMessageDialog(null, "Usuário ** " + nome + " ** Excluido com Sucesso!!!");
                }
            }
        } catch (NumberFormatException | HeadlessException e) {
            JOptionPane.showMessageDialog(null, "Erro ao tentar excluir usuário", "Atenção!!", JOptionPane.YES_NO_OPTION);
        }
    }//GEN-LAST:event_btExcluirActionPerformed
    public static void populaCampos(Long idUsuario) {
        Usuarios usuario = UsuarioDao.retornaUsuariosPorId(idUsuario);
        if (usuario != null) {
            jTextField2.setText("" + usuario.getId_usuario());
            jTextField1.setText(usuario.getNome_usuario());

            BtNovo.setEnabled(true);
            btSalvar.setEnabled(false);
            btEditar.setEnabled(true);
            btExcluir.setEnabled(true);
            btDesfazer.setEnabled(true);
        }
    }
    private void btSairComponentMoved(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_btSairComponentMoved
        // TODO add your handling code here:
    }//GEN-LAST:event_btSairComponentMoved

    private void btSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSairActionPerformed
        if (btSalvar.isEnabled()) {
            JOptionPane.showMessageDialog(null, "Salve seus Dados Antes de Sair!!!");
        } else {
            Index.fr_usuario = null;
            dispose();
        }
    }//GEN-LAST:event_btSairActionPerformed

    private void btDesfazerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDesfazerActionPerformed
        Utilitario.limparTodosCampos(rootPane);
        Utilitario.bloqueiaTodosCampos(rootPane);

        // oculto os campos de senha atual
        senhaAtual.setVisible(true);
        PassSenhaAtual.setVisible(true);

        //bloqueio os botoes para enabled
        btExcluir.setEnabled(false);
        btSalvar.setEnabled(false);
        btEditar.setEnabled(false);
        btDesfazer.setEnabled(false);

        alterando = false;
    }//GEN-LAST:event_btDesfazerActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private static javax.swing.JButton BtNovo;
    private javax.swing.JPasswordField PassSenhaAtual;
    private static javax.swing.JButton btDesfazer;
    private static javax.swing.JButton btEditar;
    private static javax.swing.JButton btExcluir;
    private static javax.swing.JButton btSair;
    private static javax.swing.JButton btSalvar;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPasswordField jPasswordField2;
    private javax.swing.JPasswordField jPasswordField3;
    private static javax.swing.JTextField jTextField1;
    private static javax.swing.JTextField jTextField2;
    private javax.swing.JLabel senhaAtual;
    // End of variables declaration//GEN-END:variables
}
